﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using AEM.Common;
using AEM.Utils;

namespace AEM.Data.DAL
{
    public class Global
    {
        public static string GetMaxField(string table, string field)
        {
            string condition = @"SELECT Max(" + field + ") FROM " + table;
            DataSet ds = AccessHelper.ExecuteDataSet(condition);

            if (null != ds && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                return ds.Tables[0].Rows[0].ToString();
            return "0";
        }

        public static bool Exist(string table, string field, string value)
        {
            string condition = @"SELECT * FROM " + table + " WHERE " + field + "={0}";
            if (value.isNumeric())
                condition = string.Format(condition, value);
            else
                condition = string.Format(condition, "'" + value + "'");

            List<OleDbParameter> param = new List<OleDbParameter>();
            param.Add(new OleDbParameter("@p0", value));

            DataSet ds = AccessHelper.ExecuteDataSet(condition, param.ToArray());

            if (null != ds && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                return true;
            return false;
        }

        public static bool Delete(string table, string field, string value)
        {
            string condition = @"DELETE * FROM " + table + " WHERE " + field + "={0}";
            if (value.isNumeric())
                condition = string.Format(condition, value);
            else
                condition = string.Format(condition, "'" + value + "'");
            List<OleDbParameter> param = new List<OleDbParameter>();
            param.Add(new OleDbParameter("@p0", value));

            int ret = AccessHelper.ExecuteNonQuery(condition, param.ToArray());
            if (ret > 0) return true;
            return false;
        }

        public static bool Delete(string table)
        {
            string condition = @"DELETE FROM " + table;

            int ret = AccessHelper.ExecuteNonQuery(condition);
            if (ret > 0) return true;
            return false;
        }

        public static DataTable List(string table, string condition)
        {
            try
            {
                condition = condition.TrimStart();
                if (!string.IsNullOrEmpty(condition) && !condition.StartsWith("WHERE", System.StringComparison.CurrentCultureIgnoreCase))
                    condition = " WHERE " + condition;
                condition = @"SELECT * FROM " + table + condition;
                DataSet ds = AccessHelper.ExecuteDataSet(condition);

                if (null != ds && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    return ds.Tables[0];
                return null;
            }
            catch (Exception ex)
            {
                LogHelper.Log(condition + "异常:" + ex.ToString());
                return null;
            }
        }

        public static bool Update(string table, string field, string value, string byfield, string byvalue)
        {
            string condition = @"UPDATE [" + table + "] SET [" + field + "]='" + value + "' WHERE [" + byfield + "]='" + byvalue + "'";
            int ret = AccessHelper.ExecuteNonQuery(condition);
            if (ret > 0) return true;
            return false;
        }
    }
}
